<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="推荐人" prop="recommendedby">
        <el-input
          v-model="queryParams.recommendedby"
          placeholder="请输入推荐人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">n

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['commendSys:equipmentrecommendation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['commendSys:equipmentrecommendation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['commendSys:equipmentrecommendation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentrecommendationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table :data="props.row.equipmentHubList" :row-class-name="rowEquipmentHubIndex" ref="equipmentHub">
            <el-table-column label="装备图片" align="center" prop="imageurl" >
              <template slot-scope="scope">
                <image-preview :src="scope.row.imageurl" :width="120" :height="120" />
              </template>
            </el-table-column>

            <el-table-column label="装备品牌" align="center" prop="brand" />
            <el-table-column label="装备型号" align="center" prop="model" />
            <el-table-column label="价格" align="center" prop="price" />
            <el-table-column label="库存数量" align="center" prop="stock" />
          </el-table>
        </template>
      </el-table-column>

      <el-table-column label="推荐人" align="center" prop="recommendedby" />
      <el-table-column label="推荐理由" align="center" prop="recommendationtext" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['commendSys:equipmentrecommendation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['commendSys:equipmentrecommendation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改装备推荐对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-divider content-position="center">装备列表信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddEquipmentHub">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteEquipmentHub">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="equipmentHubList" :row-class-name="rowEquipmentHubIndex" @selection-change="handleEquipmentHubSelectionChange" ref="equipmentHub">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="类别ID" prop="CategoryID" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.CategoryID" placeholder="请输入类别ID" />
            </template>
          </el-table-column>
          <el-table-column label="装备品牌" prop="Brand" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.Brand" placeholder="请输入装备品牌" />
            </template>
          </el-table-column>
          <el-table-column label="装备型号" prop="Model" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.Model" placeholder="请输入装备型号" />
            </template>
          </el-table-column>
          <el-table-column label="价格" prop="Price" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.Price" placeholder="请输入价格" />
            </template>
          </el-table-column>
          <el-table-column label="库存数量" prop="Stock" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.Stock" placeholder="请输入库存数量" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="CreatedAt" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.CreatedAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="装备信息更新时间" prop="UpdatedAt" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.UpdatedAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择装备信息更新时间" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEquipmentrecommendation, getEquipmentrecommendation, delEquipmentrecommendation, addEquipmentrecommendation, updateEquipmentrecommendation } from "@/api/commendSys/equipmentrecommendation";

export default {
  name: "Equipmentrecommendation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedEquipmentHub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 装备推荐表格数据
      equipmentrecommendationList: [],
      // 装备列表表格数据
      equipmentHubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recommendedby: null,
        recommendationtext: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询装备推荐列表 */
    getList() {
      this.loading = true;
      listEquipmentrecommendation(this.queryParams).then(response => {
        console.log("装备推荐列表数据:", response); // 打印获取的数据
        this.equipmentrecommendationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recommendationid: null,
        equipmentid: null,
        userid: null,
        recommendedby: null,
        recommendationtext: null,
        createdat: null
      };
      this.equipmentHubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recommendationid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recommendationid = row.recommendationid || this.ids;
      getEquipmentrecommendation(recommendationid).then(response => {
        this.form = response.data;
        this.equipmentHubList = response.data.equipmentHubList;
        this.open = true;
        this.title = "修改装备推荐";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.equipmentHubList = this.equipmentHubList;
          if (this.form.recommendationid != null) {
            updateEquipmentrecommendation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipmentrecommendation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recommendationids = row.recommendationid || this.ids;
      this.$modal.confirm('是否确认删除装备推荐编号为"' + recommendationids + '"的数据项？').then(function() {
        return delEquipmentrecommendation(recommendationids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 装备列表序号 */
    rowEquipmentHubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 装备列表添加按钮操作 */
    handleAddEquipmentHub() {
      let obj = {};
      obj.CategoryID = "";
      obj.Brand = "";
      obj.Model = "";
      obj.Specifications = "";
      obj.Price = "";
      obj.Stock = "";
      obj.ImageURL = "";
      obj.Description = "";
      obj.CreatedAt = "";
      obj.UpdatedAt = "";
      this.equipmentHubList.push(obj);
    },
    /** 装备列表删除按钮操作 */
    handleDeleteEquipmentHub() {
      if (this.checkedEquipmentHub.length == 0) {
        this.$modal.msgError("请先选择要删除的装备列表数据");
      } else {
        const equipmentHubList = this.equipmentHubList;
        const checkedEquipmentHub = this.checkedEquipmentHub;
        this.equipmentHubList = equipmentHubList.filter(function(item) {
          return checkedEquipmentHub.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleEquipmentHubSelectionChange(selection) {
      this.checkedEquipmentHub = selection.map(item => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('commendSys/equipmentrecommendation/export', {
        ...this.queryParams
      }, `equipmentrecommendation_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
